/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.net.URL;
import javax.sound.sampled.*;

/**
 *
 * @author pipo
 */
public class SoundManager {
    	
        
	private Clip clip;
        private static boolean soundsOn = true;
        
	
	// Change file name to match yours, of course
        public static SoundManager s1 = new SoundManager("/Sound/game.wav");
        public static SoundManager s2 = new SoundManager("/Sound/fight.wav");
        public static SoundManager s3 = new SoundManager("/Sound/gameover.wav");
        public static SoundManager s4 = new SoundManager("/Sound/pokemon.wav");
        public static SoundManager s5 = new SoundManager("/Sound/congratz.wav");
	
	public SoundManager (String fileName) {
		try {
                        URL soundUrl = this.getClass().getClassLoader().getResource(fileName);
                        clip = AudioSystem.getClip();
                        clip.open(AudioSystem.getAudioInputStream(getClass().getResource(fileName)));

		} catch (Exception e) {
			e.printStackTrace();
		}
                
	}

	public void play() {
		try {
			if (clip != null) {
				new Thread() {
					public void run() {
						synchronized (clip) {
							clip.stop();
							clip.setFramePosition(0);
							clip.start();
						}
					}
				}.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stop(){
		if(clip == null) return;
		clip.stop();
	}
	
	public void loop() {
		try {
			if (clip != null && soundsOn == true) {
				new Thread() {
					public void run() {
						synchronized (clip) {
							clip.stop();
							clip.setFramePosition(0);
							clip.loop(Clip.LOOP_CONTINUOUSLY);
						}
					}
				}.start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean isActive(){
		return clip.isActive();
	}
    
        public static void stopMusic()
        {
            soundsOn = false;
            s1.stop();
            s2.stop();
            s3.stop();
            s4.stop();
            s5.stop();
        }
    
        public static void startMusic(boolean b)
        {
            soundsOn = true;            
            if(b)
                s4.loop();
            else
                s1.loop();
            
        }
        
}
